//
//  MusicSelectMonthViewController.m
//  One
//
//  Created by mac on 16/6/2.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MusicSelectMonthViewController.h"
#import "MusicRequest.h"
#import "MonthMusicViewTableViewCell.h"
#import "MusicByMonth.h"
#import "MusicPlay.h"
#import "MusicViewWithTableViewController.h"

@interface MusicSelectMonthViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    NSArray *dataArr;
    UITableView *myTable;
    MusicPlay *play;
}

@end

static NSString *const cellReuseId = @"cell";

@implementation MusicSelectMonthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    dataArr = [NSArray array];
    play = [MusicPlay shareAVPlay];
    self.title = _monthStr;
    myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    myTable.delegate = self;
    myTable.dataSource = self;
    [myTable registerNib:[UINib nibWithNibName:@"MonthMusicViewTableViewCell" bundle:nil] forCellReuseIdentifier:cellReuseId];
    myTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview: myTable];
    
    [MusicRequest musicBackListWithMonthDay:_monthStr withBlock:^(NSArray *monthMusicArr) {
        dataArr = monthMusicArr;
        [myTable reloadData];
        }
    ];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicByMonth *monthObj = dataArr[indexPath.row];
    MonthMusicViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseId forIndexPath:indexPath];
    [cell.musicCover sd_setImageWithURL:[NSURL URLWithString:monthObj.cover]];
    cell.musicName.text = monthObj.title;
    cell.musicion.text = monthObj.user_name;
    cell.coverButton.tag = indexPath.row;
    [cell.coverButton addTarget:self action:@selector(musicButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

- (void)musicButtonClick:(UIButton *)sender
{
    MusicByMonth *monthObj = dataArr[sender.tag];
    [MusicRequest musicDetailRequestWithMusicId:monthObj.ID withBlock:^(MusicModleObject *obj) {
        [play playMusicWithUrl:[NSURL URLWithString:monthObj.music_id]CurrentPlayItem:obj];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicByMonth *monthObj = dataArr[indexPath.row];
    MusicViewWithTableViewController *detailVC = [[MusicViewWithTableViewController alloc]init];
    [MusicRequest musicDetailRequestWithMusicId:monthObj.ID withBlock:^(MusicModleObject *obj) {
        detailVC.musicObject = obj;
        [detailVC initialization];
        [self.navigationController pushViewController:detailVC animated:YES];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
